package com.example.springblog.interceptor;

import com.example.springblog.Utils.JWTUtils;
import com.example.springblog.constant.Constants;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从header中拿到这里token
        String token = request.getHeader(Constants.USER_TOKEN_HEADER);
        //进行解析token
        Claims claims=JWTUtils.parseJWT(token);
        if(claims == null){
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
